package com.deyuanyun.pic.eqpt.controller;

import com.deyuanyun.pic.common.dto.PageJsonBean;
import com.deyuanyun.pic.common.util.MapUtil;
import com.deyuanyun.pic.common.util.RequestUtil;
import com.deyuanyun.pic.common.util.ajax.AjaxSupport;
import com.deyuanyun.pic.eqpt.domain.EmpUse;
import com.deyuanyun.pic.eqpt.service.EmpUseService;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.List;

/**
 * Created by Bill.li on 2015/12/30.  内部借用
 */
@Controller
@RequestMapping("/eqpt/equipment/empUse")
public class EmpUseController extends EqptBaseController {
    @Resource
    private EmpUseService empUseService;

    @RequestMapping(value = "/empUsePage.html", method = RequestMethod.GET)
    public ModelAndView toInternallyPage(ModelAndView mav) {//主页
        mav.setViewName("/eqpt/empUseManagement");
        return mav;
    }

    @RequestMapping("/getEmpUse.json")
    @ResponseBody
    public void getInternally(HttpServletRequest request) {
    	HashMap map=RequestUtil.parseRequestParamToHashMap(request);
        PageJsonBean jsonBean=new PageJsonBean();
        List<EmpUse> equipmentInfoList= empUseService.getInternally(map);
        jsonBean.setList(equipmentInfoList);
        AjaxSupport.sendSuccessText("success",jsonBean);
    }

    @ResponseBody
    @RequestMapping(value = "/addEmpUse", method = RequestMethod.POST)
    public void addInternally(EmpUse info, HttpServletResponse response) {
       
        ModelAndView mav = new ModelAndView();
        try {
        	HashMap param=(HashMap)MapUtil.beanToMap(info);
			boolean status=  empUseService.addInternally(param);
			if(status)
			{
				mav.addObject("status", "0");
			}else{
				mav.addAllObjects(param);
				mav.addObject("status", "1");
			}
		} catch (Exception e) {
			e.printStackTrace();
			mav.addObject("status", "1");
		}
		AjaxSupport.sendSuccessText("ok",mav.getModel());
    }

    @ResponseBody
    @RequestMapping(value = "/updateEmpUse", method = RequestMethod.POST)
    public void updateInternally(EmpUse info, HttpServletResponse response) {
       

        ModelAndView mav = new ModelAndView();
        try {
        	HashMap param=(HashMap)MapUtil.beanToMap(info);
			boolean status=  empUseService.updateInternally(param);
			if(status)
			{
				mav.addObject("status", "0");
			}else{
				mav.addAllObjects(param);
				mav.addObject("status", "1");
			}
		} catch (Exception e) {
			e.printStackTrace();
			mav.addObject("status", "1");
		}
		AjaxSupport.sendSuccessText("ok",mav.getModel());
    }


    @RequestMapping(value = "/delEmpUse.json", method = RequestMethod.POST)
    @ResponseBody
    public void delInternally(HttpServletRequest request) {
        HashMap data = RequestUtil.parseRequestParamToHashMap(request);
        ModelAndView mav = new ModelAndView();
        try {
			boolean status=  empUseService.delInternally(data);
			if(status)
			{
				mav.addObject("status", "0");
			}else{
				mav.addObject("status", "1");
			}
		} catch (Exception e) {
			e.printStackTrace();
			mav.addObject("status", "1");
		}
		AjaxSupport.sendSuccessText("ok",mav.getModel());
    }
}
